.h файл:
class CPlane
{
public:
CVector n;
float D;
public:
CPlane();
CPlane(const CVector& nv, float _D);
CPlane(const CVertex& V1, const CVertex& V2,
const CVertex& V3);
virtual ~CPlane();
CPlane& operator = (const CPlane& Plane);
bool operator == (const CPlane& Plane) const;
bool operator != (const CPlane& Plane) const;
bool LineIntersect(const CVertex& V1, const CVertex& V2,
unsigned LineType, CVertex* VResult) const;
};
.cpp файл:
CPlane::CPlane(): n(0, 0, 0), D(0){};
CPlane::CPlane(const CVector& nv, float _D=0): n(nv), D(_D){};
CPlane::CPlane(const CVertex& V1, const CVertex& V2,
const CVertex& V3):
n(CVector(V1,V2)|CVector(V1,V3)),
D(-n.x*V1.x-n.y*V1.y-n.z*V1.z)
{n.Normalize();};
CPlane::~CPlane(){};
CPlane& CPlane::operator = (const CPlane& Plane)
{
n = Plane.n;
D = Plane.D;
return *this;
}
bool CPlane::operator == (const CPlane& Plane) const
{
return (n == Plane.n && D == Plane.D);
}
bool CPlane::operator != (const CPlane& Plane) const
{
return !(*this == Plane);
}
bool CPlane::LineIntersect(const CVertex& V1, const CVertex& V2,
unsigned LineType, CVertex* VResult) const
{
CVector Dir(V1, V2);
const float proj = n ^ Dir;
if (proj == 0) return false; //Parallel
float p;
CVertex V = CVertex(CVector(V1) -
Dir * (DistanceToPlane(V1) / proj));
if (V2.x - V1.x != 0) p=(V.x - V1.x)/(V2.x - V1.x); else
if (V2.y - V1.y != 0) p=(V.y - V1.y)/(V2.y - V1.y); else
if (V2.z - V1.z != 0) p=(V.z - V1.z)/(V2.z - V1.z); else
return false;
bool check = false;
if (LineType==LT_LINE) check = true; else
if (LineType==LT_RAY && p>=0) check = true; else
if (LineType==LT_SEGMENT && p>=0 && p<=1) check = true; else
return false;
if (check)
{
*VResult = V;
return true;
}
else
{
return false;
}
}
В класс CVertex добавляем функции для определения расстояния до плоскости и проверки
принадлежности точки плоскости:
float CVertex::DistanceTo(const CPlane& pl) const
{
return ABS((pl.n ^ *this) - pl.D);
}
bool CVertex::IsInPlane(const CPlane& pl)const
{
if ((x*pl.n.x + y*pl.n.y + z*pl.n.z + pl.D)==0)
return true;
else
return false;
}
Файл со всеми классами геометрических операций можно найти, например, в главе
шаблоны для OpenGL.